﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.WeixinMP.DataAccess;
using Microhard.WeixinMP.DTO;
using Microhard.Core;

namespace Microhard.WeixinMP.Impls
{
    public class ConversationImpl : IConversation
    {
        public Core.DBUtility.ExecutedResult Add(Models.ConversationModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            Conversation op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            op.UpdateTime = DateTime.Now;
            op.CloseTime = DateTime.Now;

            try
            {
                db.Conversations.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add("ConversationImpl.Add", "会话添加失败");
                Log.Error("ConversationImpl.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.ConversationModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            Conversation entity = db.Conversations.SingleOrDefault(c => c.ConversationID == model.ConversationID);

            if (entity != null)
            {
                entity.ProcessStep = model.ProcessStep;
                entity.UpdateTime = DateTime.Now;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                rtn.ErrorMsg.Add("ConversationImpl.Update", "会话修改失败");
                Log.Error("ConversationImpl.Update", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var list = db.Conversations.Where(c => ids.Contains(c.ConversationID)).ToList();

            list.ForEach(i => {
                i.IsFinished = true;
                i.CloseTime = DateTime.Now;
            });
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                rtn.ErrorMsg.Add("ConversationImpl.Delete", "会话删除失败");
                Log.Error("ConversationImpl.Delete", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Delete(Models.ConversationModel model)
        {
            throw new NotImplementedException();
        }

        public Models.ConversationModel GetModelByID(Guid id)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            var entity = db.Conversations.SingleOrDefault(m => m.ConversationID == id);
            if (entity != null)
                return entity.ToModel();
            return null;
        }

        public Models.ConversationModel GetModelByModel(Models.ConversationModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.ConversationModel> Query(Models.ConversationModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Models.ConversationModel> Query(Models.ConversationModel Model)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            return db.Conversations.Where(m => m.WeixinName == Model.WeixinName 
                && m.IsFinished == Model.IsFinished && m.Type == Model.Type).ToList().Select(m=>m.ToModel()).ToList();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
